<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined( '_JEXEC' ) or die( 'Restricted access' );

class iJoomla_SurveysControllerSurveys extends iJoomla_SurveysController{
	
	function __construct() {	  
		parent::__construct();
		// Register Extra tasks		
		$this->registerTask('', 'surveys');
		$this->registerTask('apply', 'apply');
		$this->registerTask('save', 'save');
		$this->registerTask('new', 'newSurvey');
		$this->registerTask('edit', 'editSurvey');		
		$this->registerTask('publish', 'publish_unpublish');
		$this->registerTask('unpublish', 'publish_unpublish');
		$this->registerTask('remove', 'remove');
		$this->registerTask('saveorder', 'saveorder');
		$this->registerTask('orderup', 'orderup');
		$this->registerTask('orderdown', 'orderdown');
		$this->registerTask('unshowresults', 'showresults');
		$this->registerTask('showresults', 'showresults');
		$this->registerTask('accessregistered', 'access');
		$this->registerTask('accesspublic', 'access');
		$this->registerTask('copy', 'copySurvey');
		$this->registerTask('clear', 'clearSurvey');
	}			
	
	function surveys(){		
		JRequest::setVar( 'view', 'Surveys' );	
		parent::display();
	}
	
	function copySurvey(){
		$model = $this->getModel('surveys');
		$model->copySurvey();
		$link = "index.php?option=com_surveys&controller=surveys";
		$this->setRedirect($link);
	}
	
	function clearSurvey(){
		$model = $this->getModel('surveys');
		$model->clearSurvey();
		$link = "index.php?option=com_surveys&controller=surveys";
		$this->setRedirect($link);
	}
	
	function access(){
		$model = $this->getModel('surveys');
		$model->access();		
		$link = "index.php?option=com_surveys&controller=surveys";
		$this->setRedirect($link);
	}
	
	function showresults(){
		$model = $this->getModel('surveys');
		$response = $model->showresults();		
		$link = "index.php?option=com_surveys&controller=surveys";
		$this->setRedirect($link);		
	}
	
	function orderup(){
		$model = $this->getModel('surveys');
		$response = $model->orderup();		
		$link = "index.php?option=com_surveys&controller=surveys";
		if ($response === false) {
			$msg = JText::_("COM_SURVEYS_ORDERUP_UNSUCCESSFULLY");
			$this->setRedirect($link, $msg, 'notice');
		} 
		elseif($response === true) {
			$msg = JText::_("COM_SURVEYS_ORDERUP_SUCCESSFULLY");		
			$this->setRedirect($link, $msg);
		}
	}
	
	function orderdown(){
		$model = $this->getModel('surveys');
		$response = $model->orderdown();		
		$link = "index.php?option=com_surveys&controller=surveys";
		if ($response === false) {
			$msg = JText::_("COM_SURVEYS_ORDERDOWN_UNSUCCESSFULLY");
			$this->setRedirect($link, $msg, 'notice');
		} 
		elseif($response === true) {
			$msg = JText::_("COM_SURVEYS_ORDERDOWN_SUCCESSFULLY");	
			$this->setRedirect($link, $msg);
		}
	}
	
	function saveorder(){
		$model = $this->getModel('surveys');
		$response = $model->saveorder();		
		$link = "index.php?option=com_surveys&controller=surveys";
		if ($response === false) {
			$msg = JText::_("COM_SURVEYS_SAVEORDER_UNSUCCESSFULLY");
			$this->setRedirect($link, $msg, 'notice');
		} 
		elseif($response === true) {
			$msg = JText::_("COM_SURVEYS_SAVEORDER_SUCCESSFULLY");		
			$this->setRedirect($link, $msg);
		}
	}
	
	function editSurvey(){
		$app = JFactory::getApplication('administrator');
		$cids = JRequest::getVar('cid', array(0));
		$app->redirect('index.php?option=com_surveys&controller=newsurvey&id='.$cids[0]);
	}
	
	function newSurvey(){
		$app = JFactory::getApplication('administrator');
		$app->redirect('index.php?option=com_surveys&controller=newsurvey');
	}	
		
	function publish_unpublish(){
		$model = $this->getModel('surveys');
		$response = $model->publish_unpublish();		
		$link = "index.php?option=com_surveys&controller=surveys";
		$task = JRequest::getVar("task", "");
		
		if($task == "publish"){
			if ($response === false) {
				$msg = JText::_("COM_SURVEYS_PUBLISH_UNSUCCESSFULLY");
				$this->setRedirect($link, $msg, 'notice');
			} 
			elseif($response === true) {
				$msg = JText::_("COM_SURVEYS_PUBLISH_SUCCESSFULLY");		
				$this->setRedirect($link, $msg);
			}
		}
		else{
			if ($response === false) {
				$msg = JText::_("COM_SURVEYS_UNPUBLISH_UNSUCCESSFULLY");
				$this->setRedirect($link, $msg, 'notice');
			} 
			elseif($response === true) {
				$msg = JText::_("COM_SURVEYS_UNPUBLISH_SUCCESSFULLY");		
				$this->setRedirect($link, $msg);
			}
		}
	}
	
	function remove(){
		$model = $this->getModel('surveys');
		$response = $model->remove();		
		$link = "index.php?option=com_surveys&controller=surveys";
		if ($response === false) {
			$msg = JText::_("COM_SURVEYS_DELETE_UNSUCCESSFULLY");
			$this->setRedirect($link, $msg, 'notice');
		} 
		elseif($response === true) {
			$msg = JText::_("COM_SURVEYS_DELETE_SUCCESSFULLY");		
			$this->setRedirect($link, $msg);
		}
	}
}

?>